<?php
/**
 * Created by PhpStorm.
 * User: zcs
 * Date: 17/2/3
 * Time: 下午2:38
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Class personal
 * 自动执行程序
 */

class Auto extends MY_Controller {

    function __construct(){
        parent::__construct();
        $this->load->model('needs_model','mneeds');
        $this->load->model('claim_model','mclaim');
        $this->load->model('need_order_model','mneedorder');
        $this->load->model('balance_detail_model','mbalance');
    }

    /**
     * 自动关闭订单（未支付、修改支付状态）、倒计时处理（可申请延时）
     */
    public function index(){
        $key = $this->input->get('key');
        if(isset($key) && $key=='bocadmin999'){
            logfile('开始处理','auto_logs');
            $s_time = time();
            $time = $s_time-24*3600;
            $no_do_orders = $this->db->query("select * from boc_needs where created < $time and status=0 and type=1")->result_array();
            if(!empty($no_do_orders)){
                foreach($no_do_orders as $val){
                    $order_id = $val['id'];
                    $updated = time();
                    $this->db->query("update boc_needs set status=3,claim_status=8,updated=$updated where id=$order_id");
                    logfile('悬赏需求'.$order_id.'处理完成','auto_logs');
                }
            }
            logfile('结束处理','auto_logs');
            $this->delay_needs();
            $this->assign_needs();
            $this->end_needs();
        }
    }

    /**
     * 延时处理
     */
    public function delay_needs(){
        logfile('开始处理','delay_logs');
        $s_time = time();
        $time = $s_time-3600;
        $no_do_orders = $this->db->query("select * from boc_needs where countdown >= $time and status=1 and type=1 and claim_status=1 and is_delay!=1")->result_array();
        if(!empty($no_do_orders)) {
            foreach ($no_do_orders as $val) {
                $order_id = $val['id'];
                $updated = time();
                $this->db->query("update boc_needs set is_delay=1,updated=$updated where id=$order_id");
                logfile('悬赏需求' . $order_id . '处理完成', 'delay_logs');
            }
        }
        logfile('结束处理','delay_logs');
    }

    /**
     * 结束活动
     */
    public function end_needs(){
        logfile('开始处理','end_logs');
        $e_time = time();
        $no_do_orders = $this->db->query("select * from boc_needs where end_time < $e_time and status=1 and type=2 and claim_status=1")->result_array();
        if(!empty($no_do_orders)) {
            foreach ($no_do_orders as $val) {
                $order_id = $val['id'];
                $updated = time();
                $this->db->query("update boc_needs set claim_status=7,updated=$updated where id=$order_id");   //活动结束已关闭
                /*******添加推送板块**********/
                $to_uid = $val['uid'];
                $t_type = 1;
                $content = '您发布的活动报名已结束';
                getui_push($to_uid,$t_type,$order_id,$content);
                logfile('悬赏需求' . $order_id . '处理完成', 'delay_logs');
            }
        }
        logfile('结束处理','delay_logs');
    }

    /**
     * 认领时间到处理
     */
    public function assign_needs(){
        logfile('开始处理','assign_logs');
        $time = time();
        //无人认领就自动关闭  余额退回
        $no_do_orders = $this->db->query("select * from boc_needs where countdown < $time and status=1 and type=1 and claim_status=1")->result_array();
        if(!empty($no_do_orders)) {
            foreach ($no_do_orders as $val) {
                $order_id = $val['id'];
                //查询该需求任务有咩有人认领 无人认领就自动关闭掉  余额退回发布方  修改明细记录
                $claim_info = $this->db->query("select * from boc_claim where need_id=$order_id")->result_array();
                $updated = time();
                if(!empty($claim_info)){
                    $this->db->query("update boc_needs set claim_status=2,updated=$updated where id=$order_id");
                    /*******添加推送板块**********/
                    $to_uid = $val['uid'];
                    $t_type = 1;
                    $content = '您发布的任务认领已结束';
                    getui_push($to_uid,$t_type,$order_id,$content);
                }else{
                    //无人认领的状态
                    $user_id = $val['uid'];
                    $all_money = $val['margin'];
                    $this->db->query("update boc_account set balance=balance+$all_money where id=$user_id");
                    $this->mbalance->update(array('status'=>5,'updated_at'=>time()),array('id'=>$val['id']));   //任务关闭已退回
                    $balance_data_in2 = array(
                        'uid'=>$user_id,
                        'type'=>6,              //悬赏全额退回
                        'title'=>$val['intro'],
                        'need_id'=>$val['id'],
                        'money'=>$all_money,
                        'status'=>6,            //任务关闭退回
                        'pay_ways'=>3,          //余额收入
                        'pay_time'=>time(),
                        'created_at'=>time(),
                        'updated_at'=>time(),
                        'rid'=>0,
                        'payment_sn'=>$val['payment_sn'],
                        'pay_code'=>$val['order_sn'],
                    );
                    $this->mbalance->create($balance_data_in2);

                    $this->db->query("update boc_needs set claim_status=7,updated=$updated where id=$order_id");
                }
                logfile('悬赏需求' . $order_id . '处理完成', 'assign_logs');
            }
        }
        logfile('结束处理','assign_logs');
    }
}
